package com.cucheng.chatroom.service.impl;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.cucheng.chatroom.model.vo.ResponseJson;
import com.cucheng.chatroom.service.FileUploadService;
import com.cucheng.chatroom.util.FileUtils;

@Service
public class FileUploadServiceImpl implements FileUploadService {

    private final static String SERVER_URL_PREFIX = "http://localhost:8080/WebSocket/";
    private final static String FILE_STORE_PATH   = "UploadFile";
    private static final Logger LOGGER            = LoggerFactory.getLogger (FileUploadServiceImpl.class);

    /**
     *@Description: TODO(文件上传) 
     *@Author: wangchengjie
     *@Since: 2019年3月30日下午3:34:53
     *@param file
     *@param request
     *@return
     */
    @Override
    public ResponseJson upload(MultipartFile file,HttpServletRequest request){
        try {
            // 重命名文件，防止重名
            String filename = getRandomUUID ();
            String suffix = "";
            String originalFilename = file.getOriginalFilename ();
            String fileSize = FileUtils.getFormatSize (file.getSize ());
            // 截取文件的后缀名
            if (originalFilename.contains (".")) {
                suffix = originalFilename.substring (originalFilename.lastIndexOf ("."));
            }
            filename = filename + suffix;
            String prefix = request.getSession ().getServletContext ().getRealPath ("/") + FILE_STORE_PATH;
            System.out.println ("存储路径为:" + prefix + "\\" + filename);
            Path filePath = Paths.get (prefix, filename);
            try {
                Files.copy (file.getInputStream (), filePath);
            } catch (IOException e) {
                e.printStackTrace ();
                return new ResponseJson ().error ("文件上传发生错误！");
            }
            return new ResponseJson ().success ().setData ("originalFilename", originalFilename).setData ("fileSize", fileSize).setData ("fileUrl",
                    SERVER_URL_PREFIX + FILE_STORE_PATH + "\\" + filename);
        } catch (Exception e) {
            e.printStackTrace ();
            LOGGER.error (e.getMessage ());
        }
        return null;
    }

    private String getRandomUUID(){
        return UUID.randomUUID ().toString ().replace ("-", "");
    }

}
